package com.zjxf.controller;


import com.zjxf.base.bean.po.PmsAsProductCollection;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.controller.common.BaseController;
import com.zjxf.service.PmsAsProductCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户收藏管理Controller
 * Created by lgw on 2021/3/30.
 */
@Api(tags = "CollectionController", description = "用户收藏管理")
@AllArgsConstructor
@RestController
@RequestMapping("/app/collection/")
public class CollectionController extends BaseController {

    private final PmsAsProductCollectionService pmsAsProductCollectionService;

    /**
     * 收藏商品
     *
     * @param productId 商品编号
     * @return JSONResponse
     */
    @ApiOperation("收藏商品")
    @PostMapping(value = "collect/{productId}")
    public JSONResponse collectProduct(@NotNull @PathVariable Long productId) {
        PmsAsProductCollection form = new PmsAsProductCollection();
        form.setProductId(productId);
        form.setCollectionTime(LocalDateTime.now());
        PmsAsProductCollection pmsAsProductCollection = pmsAsProductCollectionService.create(form);
        return success(pmsAsProductCollection);
    }

    /**
     * 查询所有收藏商品
     *
     * @return JSONResponse
     */
    @ApiOperation("查询所收藏的商品")
    @PostMapping(value = "findCollection")
    public JSONResponse selectCollections() {
        List<PmsAsProductCollection> page = pmsAsProductCollectionService.page();
        return success(page);
    }

    /**
     * 删除收藏商品
     *
     * @param productId 商品编号
     * @return JSONResponse
     */
    @ApiOperation("删除收藏商品")
    @PostMapping(value = "delete/{productId}")
    public JSONResponse deleteCollection(@NotNull @PathVariable Long productId) {
        PmsAsProductCollection collection = pmsAsProductCollectionService.get(productId);
        collection.setDeleteState(1);
        PmsAsProductCollection update = pmsAsProductCollectionService.update(collection);
        return success(update);
    }
}
